/*
Copyright 2008  Four Roads LLC conact@4-roads.com

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either 
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Controls;

namespace FourRoads.Links.Controls
{
	/// <summary>
	/// Summary description for ResLinkCommentAdmin.
	/// </summary>
	public class ResLinkCommentAdmin : ResLinkAdminTemplatedWebControl
	{
		#region Child Controls
		DataGrid _Coments;
		#endregion

		private ResLinkLink ThisPost
		{
			get
			{
				//Find it in the THisSection
				return ThisSection.FindLink(CSContext.Current.PostID);
			}
		}

		public ResLinkCommentAdmin()
		{
		}

		protected override void AttachChildControls()
		{
			_Coments = FindControl("CommentsGrid") as DataGrid;

			_Coments.ItemDataBound += new DataGridItemEventHandler(_Coments_ItemDataBound);
			_Coments.ItemCommand += new DataGridCommandEventHandler(_Coments_ItemCommand);
		}

		public override void DataBind()
		{
			base.DataBind();

			_Coments.DataSource = ThisPost.Comments;
			_Coments.DataBind();
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad (e);

			if(!Page.IsPostBack)
			{
				DataBind();
			}
		}

		private void _Coments_ItemDataBound(object sender, DataGridItemEventArgs e)
		{
			if (e.Item.ItemType == ListItemType.Item ||
				e.Item.ItemType == ListItemType.AlternatingItem )
			{
				ResLinkCommentRateLink ratingImageButton = e.Item.FindControl("CommentRating") as ResLinkCommentRateLink;
				HyperLink userDet = e.Item.FindControl("UserName") as HyperLink;
				Literal textComment = e.Item.FindControl("Comment") as Literal;
				Button delete =  e.Item.FindControl("LinkCommentDeleteBtn") as Button;
				Button approve =  e.Item.FindControl("LinkCommentApproveBtn") as Button;

				delete.CausesValidation = false;
				approve.CausesValidation = false;

				ResLinkComments com =  (ResLinkComments)e.Item.DataItem;

				ratingImageButton.Visible = com.CommentRating >= 0;

				ratingImageButton.Comment = com;

				textComment.Text = com.FormattedBody;
				userDet.Text = com.User.DisplayName;
				userDet.NavigateUrl = SiteUrls.Instance().UserProfile(com.User.UserID);

				approve.CommandArgument = com.PostID.ToString();
				delete.CommandArgument = com.PostID.ToString();

				if (com.IsApproved)
				{
					approve.Visible =false;
				}

				if (ModerateRight || ExtendedUserRoles.IsLinkAdministrator)
				{
					delete.Visible =true;
				}
				else
				{
					delete.Visible =false;
				}
			}
		}

		private void _Coments_ItemCommand(object source, DataGridCommandEventArgs e)
		{
			ResLinkLink lnk = ThisSection.FindLink(CSContext.Current.PostID);
			int findPostID = System.Convert.ToInt32(e.CommandArgument);

			if (lnk != null)
			{
				//Delete action and approve action
				if (e.CommandName == "Delete")
				{
					lnk.DeleteComment(ThisSection.SectionID , findPostID);
				}
				else if (e.CommandName == "Approve")
				{
					ResLinkComments com =  null;
					foreach(ResLinkComments c in lnk.Comments)
					{
						if (c.PostID == findPostID)
						{
							com = c;
							break;
						}
					}

					lnk.ApproveComment(com);
				}

				DataBind();
			}
		}
	}
}
